package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.WarehouseDto;
import com.qf.commerce.common.vo.WarehouseVo;
import com.qf.commerce.entity.Warehouse;
import com.qf.commerce.mapper.WarehouseMapper;
import com.qf.commerce.service.WarehouseService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Resource
    WarehouseMapper warehouseMapper;
    @Override
    public int addWarehouse(WarehouseDto warehouseDto) {
        Warehouse warehouse = CommonBeanUtils.copyForBean(warehouseDto, Warehouse::new);
        return warehouseMapper.insertWarehouse(warehouse);
    }

    @Override
    public int modifyWarehouse(WarehouseDto warehouseDto) {
        Warehouse warehouse = CommonBeanUtils.copyForBean(warehouseDto, Warehouse::new);
        return warehouseMapper.updateWarehouse(warehouse);
    }

    @Override
    public int deleteWarehouse(Long warehouseId) {
        return warehouseMapper.deleteWarehouse(warehouseId);
    }

    @Override
    public int deleteWarehouses(List<Long> warehouseIds) {
        return warehouseMapper.deleteWarehouses(warehouseIds);
    }

    /**
     * 发货点状态切换
     *
     * @param warehouseDto
     * @return
     */
    @Override
    public int switchDeliverStatus(WarehouseDto warehouseDto) {
        Warehouse warehouse = CommonBeanUtils.copyForBean(warehouseDto, Warehouse::new);
        if(warehouse.getDeliveryStatus()==1){
            return warehouseMapper.updateCloseDelivery(warehouse.getWarehouseId());
        }else if(warehouse.getDeliveryStatus()==0) {
            return warehouseMapper.updateOpenDelivery(warehouse.getWarehouseId());
        }
        throw new RuntimeException();
    }

    /**
     * 收货点状态切换
     *
     * @param warehouseDto
     * @return
     */
    @Override
    public int switchReceiveStatus(WarehouseDto warehouseDto) {
        Warehouse warehouse = CommonBeanUtils.copyForBean(warehouseDto, Warehouse::new);
        if(warehouse.getReceiveStatus()==1){
            return warehouseMapper.updateCloseReceive(warehouse.getWarehouseId());
        }else if(warehouse.getReceiveStatus()==0) {
            return warehouseMapper.updateOpenReceive(warehouse.getWarehouseId());
        }
        throw new RuntimeException();
    }

    @Override
    public PageInfo<WarehouseVo> queryWarehouseList() {
        return CommonBeanUtils.copyPage(PageHelper.startPage(1,10).doSelectPageInfo(()->warehouseMapper.selectWarehouseList()),
                new PageInfo<WarehouseVo>(),WarehouseVo::new);
    }
}
